package com.rachel.studyapp.view;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * Created by zhengshaorui on 2017/8/25.
 */

public class CusListView extends ListView {
    private static final String TAG = "zsr";
    public CusListView(Context context) {
        this(context,null);
    }

    public CusListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CusListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    float lastY = 0;
    float lastX = 0;
    float offertX = 0,offertY = 0;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //处理焦点事件，防止与 viewpager 冲突
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastY = ev.getY();
                lastX = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                float dy = ev.getY() - lastY;
                float dx =  ev.getX() - lastX;
                offertX += Math.abs(dx);
                offertY += Math.abs(dy);
                if (offertX > offertY){
                    //Log.d(TAG, "横向滑动");
                    return false; //父布局不处理，touch 事件交给子控件处理
                }
                lastX = ev.getX();
                lastY = ev.getY();

                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     * 处理横屏
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int orientation = getResources().getConfiguration().orientation;
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {

                return true;
            }
        }

        return super.dispatchTouchEvent(ev);
    }
}
